﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

namespace PMAF.K3Cloud.SCM.Bill.BusinessPlugIn
{
    [HotUpdate]
    [Description("销售出库单表单插件")]
    public class OutStock : AbstractBillPlugIn
    {
        /// <summary>
        /// 单据显示数据前
        /// </summary>
        /// <param name="e"></param>
        public override void AfterBindData(EventArgs e)
        {
            //base.AfterBindData(e);
            //DateTime date = Convert.ToDateTime(View.Model.GetValue("FDate"));
            //DateTime createDate = Convert.ToDateTime(View.Model.GetValue("FCreateDate"));
            //object billId = View.Model.GetPKValue();
            ////月末最后一天不加1天
            //if (billId == null)
            //{
            //    if (createDate.Month < date.Month)
            //    {
            //        date = createDate;
            //        View.Model.SetValue("FDate", date);
            //    }
            //}

            //将明细行数量、采购流水号及采购单价携带到行对应的流水号子单据体第一行
            object billId = View.Model.GetPKValue();
            SubEntryEntity purSnEntity = (SubEntryEntity)View.BillBusinessInfo.GetEntity("F_PMAF_PurSnSubEntity");
            Entity outStockEntity = View.BillBusinessInfo.GetEntity(purSnEntity.ParentEntityKey);

            //明细单据体数据集合
            DynamicObjectCollection outStockEntries = View.Model.GetEntityDataObject(outStockEntity);

            foreach (var entry in outStockEntries)
            {
                decimal mustQty = Convert.ToDecimal(entry["MustQty"]);
                DynamicObject purchaseSn = entry["F_PMAF_PurchaseSN"] as DynamicObject;

                //流水号子单据体数据集合
                var purSnEntries = (DynamicObjectCollection)purSnEntity.DynamicProperty.GetValue(entry);
                foreach(var purSnEntry in purSnEntries)
                {
                    decimal saleQty = Convert.ToDecimal(purSnEntry["F_PMAF_SaleQty"]);
                    //如果有数据，就不更新
                    if (saleQty.Equals(decimal.Zero))
                    {
                        purSnEntry["F_PMAF_SaleQty"] = entry["MustQty"];
                        purSnEntry["F_PMAF_DetailPurchasePrice"] = entry["F_PMAF_PurPrice"];
                        purSnEntry["F_PMAF_DetailPurchaseSn"] = purchaseSn;
                        if(purchaseSn != null)
                        {
                            purSnEntry["F_PMAF_DetailPurchaseSn_Id"] = purchaseSn["Id"];
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 值更新事件
        /// </summary>
        /// <param name="e"></param>
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);

            //明细行流水号变更，自动更新采购流水号子单据体的采购流水号和采购单价
            if (e.Field.Key == "F_PMAF_PurchaseSN")
            {
                DynamicObject purchase = (DynamicObject)Model.GetValue("F_PMAF_PURCHASESN",e.Row);
                decimal purPrice = Convert.ToDecimal(Model.GetValue("F_PMAF_PurPrice", e.Row));
                if (purchase != null)
                {
                    Model.SetValue("F_PMAF_DetailPurchaseSn", purchase);
                    Model.SetValue("F_PMAF_DetailPurchaseSn_Id", purchase["Id"]);
                    Model.SetValue("F_PMAF_DetailPurchasePrice", purPrice);
                }
            }

            //明细行采购单价变更，自动更新采购流水号子单据体的采购单价
            if (e.Field.Key == "F_PMAF_PurPrice")
            {
                decimal purPrice = Convert.ToDecimal(Model.GetValue("F_PMAF_PurPrice", e.Row));
                DynamicObject purchase = (DynamicObject)Model.GetValue("F_PMAF_PURCHASESN", e.Row);
                if (purchase != null)
                {
                    Model.SetValue("F_PMAF_DetailPurchasePrice", purPrice);
                }
            }

            //采购流水号销售数量修改，更新拆分数量
            if (e.Field.Key == "F_PMAF_SaleQty")
            {
                SubEntryEntity purSnEntity = (SubEntryEntity)View.BillBusinessInfo.GetEntity("F_PMAF_PurSnSubEntity");
                DynamicObjectCollection purSnEntries = View.Model.GetEntityDataObject(purSnEntity);
                decimal saleQty = decimal.Zero;
                foreach(var purSnEntry in purSnEntries)
                {
                    decimal currentQty = Convert.ToDecimal(purSnEntry["F_PMAF_SaleQty"]);
                    saleQty += currentQty;
                }
                int currentRowIndex = View.Model.GetEntryCurrentRowIndex("FEntity");
                
                View.Model.SetValue("F_PMAF_CutQty", saleQty, currentRowIndex);
            }
        }

        /// <summary>
        /// 单据明细菜单栏点击事件
        /// </summary>
        /// <param name="e"></param>
        public override void EntryBarItemClick(BarItemClickEventArgs e)
        {
            base.EntryBarItemClick(e);

            //点击采购流水号增加行按钮
            if (e.BarItemKey.EqualsIgnoreCase("PMAF_CutPurchaseSntb"))
            {
                //当前明细行
                int currentRowIndex = View.Model.GetEntryCurrentRowIndex("FEntity");
                EntryEntity entity = View.BusinessInfo.GetEntryEntity("FEntity");
                DynamicObject dynamicObject = View.Model.GetEntityDataObject(entity, currentRowIndex);
                decimal currentRealQty = Convert.ToDecimal(dynamicObject["RealQty"]);

                //采购流水号子单据体
                SubEntryEntity purSnEntity = (SubEntryEntity)View.BillBusinessInfo.GetEntity("F_PMAF_PurSnSubEntity");
                DynamicObjectCollection purSnEntries = View.Model.GetEntityDataObject(purSnEntity);

                //采购流水号行
                int purSnIndex = 0;

                //采购流水号销售数量
                decimal saleQty = decimal.Zero;

                //逐行统计流水号数量及更新行数
                foreach (var purSnEntry in purSnEntries)
                {
                    decimal currentQty = Convert.ToDecimal(purSnEntry["F_PMAF_SaleQty"]);
                    saleQty += currentQty;
                    purSnIndex++;
                }

                //添加行
                View.Model.CreateNewEntryRow("F_PMAF_PurSnSubEntity");

                //对新添加的行进行销售数量赋值
                Model.SetValue("F_PMAF_SaleQty", currentRealQty - saleQty, purSnIndex);
            }

            //点击采购流水号删除行按钮
            if (e.BarItemKey.EqualsIgnoreCase("PMAF_DelPurSnRow"))
            {
                //当前行
                int selectedRow = View.Model.GetEntryCurrentRowIndex("F_PMAF_PurSnSubEntity");

                //删除行
                View.Model.DeleteEntryRow("F_PMAF_PurSnSubEntity", selectedRow);
            }
        }
    }
}
